package com.wuwangfu.partition;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

/**
 * @Author jcshen
 * @Date 2023-02-23
 * @PackageName:com.wuwangfu.partition
 * @ClassName: Shuffle
 * @Description: random，随机分区，将数据随机的发送给下游subtask
 * @Version 1.0.0
 *
 * https://nightlies.apache.org/flink/flink-docs-release-1.14/docs/dev/datastream/operators/overview/#random-partitioning
 */
public class Shuffle {
    public static void main(String[] args) throws Exception {

        Configuration config = new Configuration();
        //方便观察web ui并行度
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(config);
        //并行度为1
        DataStreamSource<String> line = env.socketTextStream("localhost", 8888);
        //并行度为2
        SingleOutputStreamOperator<String> mapped = line.map(new RichMapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                int subtask = getRuntimeContext().getIndexOfThisSubtask();
                return value + ":" + subtask;
            }
        }).setParallelism(1);
        //将数据随机的发送给下游subtask
        DataStream<String> shuffled = mapped.shuffle();
        //
        shuffled.addSink(new RichSinkFunction<String>() {
            @Override
            public void invoke(String value, Context context) throws Exception {
                int subtask = getRuntimeContext().getIndexOfThisSubtask();
                System.out.println(value + "->" + subtask);
            }
        });
        env.execute();
    }
}
